package com.liuxu.topnews.service;

import android.accessibilityservice.AccessibilityService;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.accessibility.AccessibilityEvent;

import com.liuxu.topnews.Manager.ActionManager;
import com.liuxu.topnews.MyApplication;
import com.liuxu.topnews.bean.App;
import com.liuxu.topnews.bean.DialogData;
import com.liuxu.topnews.utils.DialogUtil;
import com.liuxu.topnews.utils.LogUtil;
import com.liuxu.topnews.utils.MultiUtils;
import com.liuxu.topnews.utils.RootUtils;
import com.liuxu.topnews.utils.SettingUtils;
import com.liuxu.topnews.utils.ShareUtil;
import com.liuxu.topnews.utils.ToastUtils;

import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;

import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
import static com.liuxu.topnews.MyApplication.globalapps;

public class AccessService extends AccessibilityService {
    public int search_count=3;
    private boolean autoclick;
    public DialogUtil dialogUtil;
    private static AccessService Instance;

    public static AccessService getInstance() {
        return Instance;
    }

    public void onServiceConnected(){
        Log.v("无障碍服务","打开");
        Instance=AccessService.this;
        dialogUtil =new DialogUtil();
        autoclick=true;
        Log.v("初始化", "在服务连接中设置APP");
        setapps();
    }
    public void onCreate() {
        super.onCreate();
        Log.e("Service", "onCreate");

    }
    public void onDestroy() {
        Log.e("Service", "onDestroy");
        super.onDestroy();

    }
    @Override
    public boolean onKeyEvent(KeyEvent event) {
        int key = event.getKeyCode();
        if(MyApplication.permission) {
            switch (key) {
                case KeyEvent.KEYCODE_VOLUME_DOWN:
                    LogUtil.getInstance().show("暂停运行");
                    ToastUtils.getInstance().shownow("暂停运行");
                    MyApplication.Pause=true;
                    break;
                case KeyEvent.KEYCODE_VOLUME_UP:
                    LogUtil.getInstance().show("继续运行");
                    ToastUtils.getInstance().shownow("继续运行");
                    MyApplication.Pause=false;
                    break;
            }
        }
        return super.onKeyEvent(event);
    }
    public void onAccessibilityEvent(AccessibilityEvent event) {
        int type = event.getEventType();
        if (event.getClassName() != null) {
            String classname = event.getClassName().toString();
            if (type==TYPE_WINDOW_STATE_CHANGED) {
                MyApplication.UpClassName = MyApplication.NowClassName;
                MyApplication.NowClassName = classname;
                Log.v("nowclassname==" + MyApplication.NowClassName, "窗口变动");
                if (autoclick){
                    autoclick=false;
                    try {
                        DialogData data= (DialogData)dialogUtil.dialogs.get(classname);
                        if (data!=null) {
                            switch (data.type){
                                case 0:
                                    ActionManager.getInstance().ClickText(data.action);
                                    break;
                                case 1:
                                    ActionManager.getInstance().ClickId(data.action);
                                    break;
                                case 2:
                                    RootUtils.getInstance().execShellCmd(data.action);
                                    break;
                                default:
                                    Log.v("default==" , "窗口变动");
                                    break;
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    autoclick=true;
                }
            }
            }
    }
    public int onStartCommand(Intent intent, int flags, int startId) {

        return super.onStartCommand(intent, flags, startId);
    }

    public void onInterrupt() {
    }

    public void setapps(){
        List<App> appsets=new SettingUtils().GetAppSetXmlByboolean(true);
        globalapps =new MultiUtils().initAppData(appsets);
    }
}

